####################################
# This is code to replicate the analyses and figures from the supplementary materials
# Produces Table 15 and 16
# (Un)Natural Disasters: Electoral Cycles in Disaster Relief
# Code developed by Alicia Cooperman and Andrea Junqueira
####################################
# Analysis conducted using
# R version 4.0.2 (2020-06-22) 
# on Platform: x86_64-apple-darwin17.0 (64-bit)
####################################

rm(list=ls(all=TRUE))
dev.off()
library(lfe) # Version 2.8.5
library(stargazer) # Version 5.2.2
load("cooperman_2021_SI_gov.RData")


# Table 15: Governor Vote Share and Drought Declaration -------------------

m2_reelection_drought_stateFE <- felm(perc_gov_mun ~ drought_gov + perc_gov_mun_lag + copartisan_gov + gov_pres_copartisan + lncattle_lag + corn_share_lag + bean_share_lag | as.factor(SIGLA_UF) + year|0|state.year,
                                     data = df.gov, subset = reelection==1)
m2_partyCont_drought_stateFE <- felm(perc_gov_mun ~ drought_gov + perc_gov_mun_lag + copartisan_gov + gov_pres_copartisan + lncattle_lag + corn_share_lag + bean_share_lag | as.factor(SIGLA_UF) + year|0|state.year,
                                    data = df.gov, subset = reelection==1 | same_party_current_gov==1)
m2_reelection_drought2years_stateFE <- felm(perc_gov_mun ~ drought_gov_2years + perc_gov_mun_lag + copartisan_gov + gov_pres_copartisan + lncattle_lag + corn_share_lag + bean_share_lag | as.factor(SIGLA_UF) + year|0|state.year,
                                           data = df.gov, subset = reelection==1)
m2_partyCont_drought2years_stateFE <- felm(perc_gov_mun ~ drought_gov_2years + perc_gov_mun_lag + copartisan_gov + gov_pres_copartisan + lncattle_lag + corn_share_lag + bean_share_lag | as.factor(SIGLA_UF) + year|0|state.year,
                                          data = df.gov, subset = reelection==1 | same_party_current_gov==1)

stargazer(m2_reelection_drought_stateFE,m2_partyCont_drought_stateFE,m2_reelection_drought2years_stateFE,m2_partyCont_drought2years_stateFE)

govlabels <- c("Drought Declaration (Gov. Elec. Year)", "Drought Declaration (Previous Two Years)",
                    "Governor Vote Share in Previous Election","Copartisan Mayor Governor", "Copartisan President Governor", "Cattle (lag)", "Corn (lag)", "Beans (lag)")
cat(stargazer(m2_reelection_drought_stateFE,m2_partyCont_drought_stateFE,
              m2_reelection_drought2years_stateFE,m2_partyCont_drought2years_stateFE
              ,column.labels = c("(Individual)","(Party)", "(Individual)","(Party)")
              ,dep.var.labels="Vote share received by Governor in municipality",float=FALSE, covariate.labels=govlabels
              , omit.stat=c("ll", "f", "ser"), no.space=F), file="SI_tab15.tex", sep="\n")
summary(df.gov$perc_gov_mun)

# Table 16: Governor Vote Share and Drought Declaration with Municipal FE -------------------

m2_reelection_drought_munFE <- felm(perc_gov_mun ~ drought_gov + perc_gov_mun_lag + copartisan_gov + gov_pres_copartisan + lncattle_lag + corn_share_lag + bean_share_lag | as.factor(ibgecode) + year|0|state.year,
                                      data = df.gov, subset = reelection==1)
m2_partyCont_drought_munFE <- felm(perc_gov_mun ~ drought_gov + perc_gov_mun_lag + copartisan_gov + gov_pres_copartisan + lncattle_lag + corn_share_lag + bean_share_lag | as.factor(ibgecode) + year|0|state.year,
                                     data = df.gov, subset = reelection==1 | same_party_current_gov==1)
m2_reelection_drought2years_munFE <- felm(perc_gov_mun ~ drought_gov_2years + perc_gov_mun_lag + copartisan_gov + gov_pres_copartisan + lncattle_lag + corn_share_lag + bean_share_lag | as.factor(ibgecode) + year|0|state.year,
                                           data = df.gov, subset = reelection==1)
m2_partyCont_drought2years_munFE <- felm(perc_gov_mun ~ drought_gov_2years + perc_gov_mun_lag + copartisan_gov + gov_pres_copartisan + lncattle_lag + corn_share_lag + bean_share_lag | as.factor(ibgecode) + year|0|state.year,
                                          data = df.gov, subset = reelection==1 | same_party_current_gov==1)

cat(stargazer(m2_reelection_drought_munFE,m2_partyCont_drought_munFE,m2_reelection_drought2years_munFE,m2_partyCont_drought2years_munFE
              ,column.labels = c("(Individual)","(Party)", "(Individual)","(Party)")
              ,dep.var.labels="Vote share received by Governor in municipality",float=FALSE, covariate.labels=govlabels
              , omit.stat=c("ll", "f", "ser"), no.space=F), file="SI_tab16.tex", sep="\n")

